/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thread;

import canvas.MIDPCanvasPlay;
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import pojo.ImageChicken;

/**
 *
 * @author LTTDNQ
 */
public class RunableChickenMove implements Runnable {

    //Khai báo biến
    private MIDPCanvasPlay _canvas;
    private int _viTriChickenX = 105;
    private int _viTriChickenY = 20;
    private int _intRand;
    private int[] _arrIntRand;
    private boolean _flagPlay = true;
    private ImageChicken[] _arrImgChick;
    private boolean _flagFirst = true;
    //Khởi tạo get, set

    public int getViTriChickenX() {
        return _viTriChickenX;
    }

    public void setViTriChickenX(int _viTriChickenX) {
        this._viTriChickenX = _viTriChickenX;
    }

    public int getViTriChickenY() {
        return _viTriChickenY;
    }

    public void setViTriChickenY(int _viTriChickenY) {
        this._viTriChickenY = _viTriChickenY;
    }

    public RunableChickenMove(MIDPCanvasPlay c, ImageChicken[] arr, int viTriX) {
        this._canvas = c;
        this._viTriChickenX = viTriX;
        this._arrImgChick = arr;
    }

    public void run() {
        while (!_canvas.getStop()) {
            try {
                _flagPlay = true;
                randInt();
                Thread.sleep(2000);
                _flagPlay = false;
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

    private void randInt() {
        if (_canvas.getSoGiaCam() > 1 && _canvas.getSoGiaCam() < 6) {
            _arrIntRand = new int[_canvas.getSoGiaCam()];
            int range = 21 / _canvas.getSoGiaCam();
            for (int i = 0; i < _canvas.getSoGiaCam(); i++) {
                Random rand = new Random();
                _intRand = rand.nextInt(range);
                boolean flag = false;
                do {
                    _arrIntRand[i] = (_intRand + range * i) * 10;
                    if ( _arrIntRand[i]  > i * range &&  _arrIntRand[i]  < (range * i + range) * 10 - 20) {
                        flag = true;
                    }
                } while (!flag);
            }
        } else {

            Random rand = new Random();
            _intRand = rand.nextInt(21);
        }
    }

    public void paintChicken(Graphics graphics) {
        if (_flagPlay) {
            if (_canvas.getSoGiaCam() == 6) {
                int _tempX = (240 - 30 * _canvas.getSoGiaCam()) / (1 + _canvas.getSoGiaCam());
                for (int i = 0; i < 6; i++) {
                    _canvas.getArrImage()[i].setX(_tempX * (i + 1) + 30 * (i));
                    graphics.drawImage(_arrImgChick[i].getImg(), _canvas.getArrImage()[i].getX() * _canvas.getChieuDai() / 240, _viTriChickenY * _canvas.getChieuCao() / 320, Graphics.LEFT | Graphics.TOP);
                }
            } else if (_canvas.getSoGiaCam() == 1) {
                if (_flagFirst) {
                    graphics.drawImage(_arrImgChick[0].getImg(), _canvas.getArrImage()[0].getX() * _canvas.getChieuDai() / 240, _viTriChickenY * _canvas.getChieuCao() / 320, Graphics.LEFT | Graphics.TOP);
                    _flagFirst = false;
                } else {
                    int _tempX = _intRand * 10;
                    _canvas.getArrImage()[0].setX(_tempX);
                    graphics.drawImage(_arrImgChick[0].getImg(), _canvas.getArrImage()[0].getX() * _canvas.getChieuDai() / 240, _viTriChickenY * _canvas.getChieuCao() / 320, Graphics.LEFT | Graphics.TOP);
                }
            } else {
                for (int i = 0; i < _canvas.getSoGiaCam(); i++) {

                    _canvas.getArrImage()[i].setX(_arrIntRand[i]);
                    graphics.drawImage(_arrImgChick[i].getImg(), _canvas.getArrImage()[i].getX() * _canvas.getChieuDai() / 240, _viTriChickenY * _canvas.getChieuCao() / 320, Graphics.LEFT | Graphics.TOP);
                }
            }

        }
    }
}
